#include <stdio.h>


int simple_memcmp (const char *s1, const char *s2, size_t n)
{
  int ret = 0;

  while (n--
	 && (ret = *(unsigned char *) s1++ - *(unsigned char *) s2++) == 0);
  return ret;
}

int _memcmp(const void * s1, const void * s2, size_t n)		/* size of memory to compare */
{
	const unsigned char *p1;
	const unsigned char *p2;
	
	/* size of memory is zero */
	
	if (n == 0)
		return (0);
	
	/* compare array 2 into array 1 */
	
	p1 = s1;
	p2 = s2;
	
	while (*p1++ == *p2++) {
		if (--n == 0)
			return (0);
    }
	
	return ((*--p1) - (*--p2));
}

static void _main( void )
{
	char first[]  = "12345678901234567890";
	char second[] = "12345678901234567891";
	int result;
	
	printf( "Compare '%.19s' to '%.19s':\n", first, second );
	result = _memcmp( first, second, 19 );
	if( result < 0 )
		printf( "First is less than second.\n" );
	else if( result == 0 )
		printf( "First is equal to second.\n" );
	else if( result > 0 )
		printf( "First is greater than second.\n" );

	printf( "Compare '%.20s' to '%.20s':\n", first, second );
	result = simple_memcmp( first, second, 20 );
	if( result < 0 )
		printf( "First is less than second.\n" );
	else if( result == 0 )
		printf( "First is equal to second.\n" );
	else if( result > 0 )
		printf( "First is greater than second.\n" );
}